package com.serviceprovider.business.module.admin.address.dao.impl;

//~--- non-JDK imports --------------------------------------------------------

import com.serviceprovider.business.module.admin.address.dao.interfaces.IAddressDao;
import com.serviceprovider.business.module.admin.address.dao.interfaces.IGeoDao;
import com.serviceprovider.business.module.admin.address.model.Address;
import com.serviceprovider.business.module.user.model.impl.User;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
import org.springframework.stereotype.Repository;

//~--- JDK imports ------------------------------------------------------------

import java.sql.ResultSet;
import java.sql.SQLException;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Leonardo A. Auer
 * since 04/11/2011
 */
@Repository("addressDao")
public class AddressDao extends SimpleJdbcDaoSupport implements IAddressDao {
    @Autowired
    private IGeoDao          geoDao;
    @Qualifier("addressJdbcInsert")
    @Autowired
    private SimpleJdbcInsert jdbcInsert;

    @Autowired
    public AddressDao(@Qualifier("jdbcTemplate") JdbcTemplate jdbcTemplate) {
        setJdbcTemplate(jdbcTemplate);
    }

    @Override
    public Address insertAddress(Address address) {
        Map<String, Object> parameters = new HashMap<String, Object>();

        parameters.put("street_address_1", address.getStreetAddress1());
        parameters.put("street_address_2", address.getStreetAddress2());
        parameters.put("county", address.getCounty());
        parameters.put("city", address.getCity());
        parameters.put("state", address.getState().getCode());
        parameters.put("zip", address.getZip());
        parameters.put("country", address.getCountry().getCode());
        parameters.put("fax", address.getFax());
        parameters.put("phone", address.getPhone());
        parameters.put("cell", address.getCell());

        Number id = jdbcInsert.executeAndReturnKey(parameters);

        address.setId(id.intValue());

        return address;
    }

    @Override
    public void updateAddress(Address address) {
        getSimpleJdbcTemplate().update(" UPDATE address SET " + "street_address_1 = ?, " + "street_address_2 = ?, "
                                       + "city = ?, " + "state = ?, " + "zip = ?, " + "country = ?, " + "fax = ?, "
                                       + "phone = ?, " + "cell = ? " + "WHERE id = ? ", address.getStreetAddress1(),
                                           address.getStreetAddress2(), address.getCity(),
                                           address.getState().getCode(), address.getZip(),
                                           address.getCountry().getCode(), address.getFax(), address.getPhone(),
                                           address.getCell(), address.getId());
    }

    @Override
    public void deleteAddress(int addressId) {
        getSimpleJdbcTemplate().update(" DELETE FROM address WHERE id = ? ", addressId);
    }

    @Override
    public Address findById(int id) {
        List<Address> addressList = getSimpleJdbcTemplate().query(" SELECT * FROM address WHERE id = ? ",
                                        new AddressMapper(), id);
        Address address = null;

        if (addressList.size() != 0) {
            address = addressList.get(0);
        }

        return address;
    }

    @Override
    public void retrieveAddresses(User user) {
        if (user.getContactAddress() != null) {
            user.setContactAddress(this.findById(user.getContactAddress().getId()));
        }

        if (user.getBusinessAddress() != null) {
            user.setBusinessAddress(this.findById(user.getBusinessAddress().getId()));
        }

        if (user.getShippingAddress() != null) {
            user.setShippingAddress(this.findById(user.getShippingAddress().getId()));
        }
    }

    private class AddressMapper implements ParameterizedRowMapper<Address> {
        @Override
        public Address mapRow(ResultSet rs, int arg1) throws SQLException {
            Address address = new Address();

            address.setId(rs.getInt("id"));
            address.setStreetAddress1(rs.getString("street_address_1"));
            address.setStreetAddress2(rs.getString("street_address_2"));
            address.setCounty(rs.getString("county"));
            address.setCity(rs.getString("city"));
            address.setState(geoDao.getState(rs.getString("state")));
            address.setZip(rs.getString("zip"));
            address.setCountry(geoDao.getCountry(rs.getString("country")));
            address.setFax(rs.getString("fax"));
            address.setPhone(rs.getString("phone"));
            address.setCell(rs.getString("cell"));

            return address;
        }
    }
}



